package com.iwunu.gateway.service.impl;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.iwunu.iot.common.http.HttpClientConnectionManager;
import com.iwunu.iot.common.http.SSLClient;

@Transactional
@Service
public class HttpClintService {
	private static final Logger log = LoggerFactory.getLogger(HttpClintService.class);
	public static DefaultHttpClient httpclient;
	static
	{
		httpclient = new DefaultHttpClient();
		
		httpclient = (DefaultHttpClient)HttpClientConnectionManager.getSSLInstance(httpclient);
	}
	/**
	 * HTTP转发
	 * @param json
	 * @return
	 */
	public  JSONObject sendMessage(String  param,String url){
		  JSONObject result = null;
	       HttpClient httpClient = null;
	  	   HttpPost httpost= HttpClientConnectionManager.getPostMethod(url);
	       try {
	         httpClient = new SSLClient();
	         StringEntity se = new StringEntity(param.toString(),"utf-8");
	         se.setContentType("text/json");
	         se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
	         httpost.setEntity(se);
	  		 HttpResponse response = httpClient.execute(httpost);
	  	     String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
	  	   if(response != null){
               HttpEntity resEntity = response.getEntity();
               if(resEntity != null){
            	   result = JSONObject.parseObject(jsonStr);
               }
           }
	  	} catch (Exception e) {
	  		log.error("HttpClintService sendMessage:"+param.toString());
	  		 e.printStackTrace();
	  	}finally{
	  		httpost.releaseConnection();
	  	}
	       return result;
	}
	/**
	 * HTTP转发
	 * @param json
	 * @return
	 */
	public  JSONObject sendMessage(JSONObject param,String url){
		  JSONObject result = null;
	       HttpClient httpClient = null;
	  	   HttpPost httpost= HttpClientConnectionManager.getPostMethod(url);
	       try {
	         httpClient = new SSLClient();
	         StringEntity se = new StringEntity(param.toString(),"utf-8");
	         se.setContentType("text/json");
	         se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
	         httpost.setEntity(se);
	  		 HttpResponse response = httpClient.execute(httpost);
	  	     String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
	  	   if(response != null){
               HttpEntity resEntity = response.getEntity();
               if(resEntity != null){
            	   result = JSONObject.parseObject(jsonStr);
               }
           }
	  	} catch (Exception e) {
	  		log.error("HttpClintService sendMessage:"+param.toString());
	  		 e.printStackTrace();
	  	}finally{
	  		httpost.releaseConnection();
	  	}
	       return result;
	}
	/**
	 * HTTP转发带token
	 * @param json
	 * @return
	 */
	public  JSONObject sendMessageToken(JSONObject param,String url,String token){
		  JSONObject result = null;
	       HttpClient httpClient = null;
	  	   HttpPost httpost= HttpClientConnectionManager.getPostMethod(url,token);
	       try {
	         httpClient = new SSLClient();
	         StringEntity se = new StringEntity(param.toString(),"utf-8");
	         se.setContentType("text/json");
	         se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
	         httpost.setEntity(se);
	  		 HttpResponse response = httpClient.execute(httpost);
	  	     String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
	  	   if(response != null){
               HttpEntity resEntity = response.getEntity();
               if(resEntity != null){
            	   result = JSONObject.parseObject(jsonStr);
               }
           }
	  	} catch (Exception e) {
	  		log.error("HttpClintService sendMessage:"+param.toString());
	  		 e.printStackTrace();
	  	}
	       return result;
	}
	
}
